var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/db227',function(err){
  if(err){
    console.log(err);
  }
  else{
    console.log('connect to mongodb succeed');
  }
});
/*
    我们的目标是: 智能教学!
*/
var userSchema = new Schema({
    account: {type: Number},
    /*账号,不可重复*/
    salt:{type: String},
    hash:{type: String},
    /*加盐密码*/
    name: {type: String},
    /*真实姓名*/
    sex: {type: Number},
    /*性别:女(0)和男(1)*/
    avatar: {type: String},
    /*头像url路径*/
    role: {type: Number},
    /*教学角色，目前有老师(0)和学生(1)两种*/
    socketId: {type: String}
    /*在线时的通信管道标示符,只记录最新连接的socketId*/
});
var userModel = mongoose.model('user',userSchema);
exports.userModel  = userModel;

var putQuestionSchema = new Schema({
  title: {type: String},
  /*问题的题目,由提问者指定*/
  time: {type: Number},
  /*问题发布的时间*/
  finished: {type: Boolean},
  /*标记该题是否已经停止作答,停止作答后公布正确答案*/
  allStudents: [String],
  /*所有作答过的学生名单*/
  choices: [{
    flag: {type: Boolean},
    /*标记该选项正确与否*/
    text: {type: String},
    /*选项的文本描述,由提问者指定*/
    students: [String]
    /*选择该选项的学生Id的列表,作为连接userShcema的外键*/
  }]
});
var putQuestionModel = mongoose.model('putQuestion',putQuestionSchema);
exports.putQuestionModel  = putQuestionModel;

var makeTeamSchema = new Schema({
  title: {type: String},
  /*组队的题目,由发起者指定*/
  time: {type: Number},
  /*组队发布的时间*/
  allStudents: [String],
  /*所有报名的学生名单*/
  finished:{type: Boolean},
  /*标记组队是否已经停止*/
  choices: [{
    text: {type: String},
    /*选项的文本描述,由发起者指定,描述该类型队伍的要求(如大作业按题目分组)*/
    groups: [{
      students: [String]
      /*选择该类型的队伍的学生Id列表,第0个即为该组的队长*/
    }],
    minGroupNum: {type: Number},
    /*指定该选项的最小队伍容量(如大作业要求每个题目最少选1组)*/
    maxGroupNum: {type: Number},
    /*指定该选项的最大队伍容量(如大作业要求每个题目最多选3组)*/
    minStudentNum: {type: Number},
    /*指定该选项的队伍的最小学生容量(如大作业要求每个队伍3-5人)
    ,组队完成后人数没到最小标准的队伍将被打乱重组*/
    maxStudentNum: {type: Number}
    /*指定该选项的队伍的最大学生容量(如大作业要求每个队伍3-5人)*/
  }]
});
var makeTeamModel = mongoose.model('makeTeam',makeTeamSchema);
exports.makeTeamModel  = makeTeamModel;

var pdfSchema = new Schema({
  url:{type: String},
  page:[{
    index:{type: Number},
    reply:[{
      userId:{type: String},
      type:{type: Number},
      title:{type: String},
      content:{type: String},
      time:{type: String},
      _time:{type: Number},
      praise:[String],
      file:{
        url:{type: String},
        name:{type: String}
      },
      //fileUrl是为之后上传附件预留的,本周先不管
      subReply:[{
        userId:{type: String},
        content:{type: String},
        time:{type: String},
        _time:{type: Number},
        praise:[String],
        comment:[{
          userId:{type: String},
          target:{type: Number},
          content:{type: String},
          time:{type: String},
          _time:{type: Number},
        }]
      }]
    }]
  }]
});
var pdfModel = mongoose.model('pdf',pdfSchema);
exports.pdfModel  = pdfModel;

var pdfUserSchema = new Schema({
  id:{type: String},
  nickname:{type: String},
  hash:{type: String},
  mobile:{type: String},
  email:{type: String},
  role:{type: Number},
  salt:{type: String},
  head:{type: String},
  token:{type: String}
});
var pdfUserModel = mongoose.model('pdfUser',pdfUserSchema);
exports.pdfUserModel  = pdfUserModel;
